package br.edu.unifei.cct720.alieninvasion.utils;

import java.awt.Component;
import java.awt.DefaultKeyboardFocusManager;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

/**
 * Class to present project messages to the user system
 * 
 * @author Felipe Knappe
 *
 */
public class Messages {

	/**
	 * Method responsible for show a confirmation message to user
	 * 
	 * @param component The parent component
	 * @param message The confirmation message
	 * 
	 */
	public static boolean confirmMessage(Component component, String message) {
		final JOptionPane j = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION, null);
		j.setComponentOrientation(((component == null) ?
				JOptionPane.getRootFrame() : component).getComponentOrientation());
		final Component componenteFoco = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		JDialog dialog = j.createDialog(component, "Confirm");
		dialog.setVisible(true);
		dialog.addWindowListener(new WindowAdapter() {

			public void windowDeactivated(WindowEvent e) {
				if(componenteFoco != null && (j.getValue().equals(JOptionPane.CLOSED_OPTION) || ((Integer)j.getValue()).equals(JOptionPane.NO_OPTION))) {
					componenteFoco.requestFocus();
				}
			}

		});
		return j.getValue().equals(JOptionPane.YES_OPTION);
	}
	
	/**
	 * Method responsible for show project failures messages to user
	 * 
	 * @param component The parent component
	 * @param failure The failure message
	 * 
	 */
	public static void showFailure(final Component component, String failure) {
		JOptionPane j = new JOptionPane(failure, JOptionPane.ERROR_MESSAGE);
		j.setComponentOrientation(((component == null) ?
				JOptionPane.getRootFrame() : component).getComponentOrientation());

		final Component componenteFoco = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		JDialog dialog = j.createDialog(component, "ERROR!");
		dialog.addWindowListener(new WindowAdapter() {

			@Override
			public void windowDeactivated(WindowEvent e) {
				if(componenteFoco != null) {
					componenteFoco.requestFocus();
				}
			}

		});
		dialog.setVisible(true);
	}
	
	/**
	 * Method responsible for show user's informative messages
	 * 
	 * @param component The parent component
	 * @param message The informative message
	 * 
	 */
	public static void showInformationMessage(final Component component, String message) {
		JOptionPane j = new JOptionPane(message, JOptionPane.INFORMATION_MESSAGE);
		j.setComponentOrientation(((component == null) ?
				JOptionPane.getRootFrame() : component).getComponentOrientation());

		final Component componenteFoco = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		JDialog dialog = j.createDialog(component, "");
		dialog.addWindowListener(new WindowAdapter() {

			@Override
			public void windowDeactivated(WindowEvent e) {
				if(componenteFoco != null) {
					componenteFoco.requestFocus();
				}
			}

		});
		dialog.setVisible(true);
	}
	
	/**
	 * Method responsible for show project authors information
	 * 
	 * @param component The parent component
	 * @param message The authors information
	 * 
	 */
	public static void showPlainMessage(final Component component, String message) {
		JOptionPane j = new JOptionPane(message, JOptionPane.PLAIN_MESSAGE);
		j.setComponentOrientation(((component == null) ?
				JOptionPane.getRootFrame() : component).getComponentOrientation());

		final Component componenteFocus = DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
		JDialog dialog = j.createDialog(component, "");
		dialog.addWindowListener(new WindowAdapter() {

			@Override
			public void windowDeactivated(WindowEvent e) {
				if(componenteFocus != null) {
					componenteFocus.requestFocus();
				}
			}

		});
		dialog.setVisible(true);
	}
	
	/**
	 * Method responsible for receive user entry values and
	 * converts it to integer value
	 * 
	 * @param message The user entry message
	 * 
	 * @return int
	 * @throws NumberFormatException
	 */
	public static int entryIntegerValues(String message) throws NumberFormatException {
		String valor = JOptionPane.showInputDialog(
				null, 
				message, 
				JOptionPane.INFORMATION_MESSAGE
		);
		int vl = 0;
		if(valor != null) {
			vl = Integer.parseInt(valor);
		}
		return vl;
	}
	
	/**
	 * Method responsible for receive user entry values and
	 * converts it to double value
	 * 
	 * @param message The user entry message
	 * 
	 * @return double
	 * @throws NumberFormatException
	 */
	public static double entryDoubleValues(String message) throws NumberFormatException {
		String value = JOptionPane.showInputDialog(
				null, 
				message, 
				JOptionPane.INFORMATION_MESSAGE
		);
		double vl = 0;
		if(value != null) {
			vl = Double.parseDouble(value);
		}
		return vl;
	}
}
